﻿using ProjScan.Tools;
using System;
using System.Linq;
using UnityEngine;

namespace ProjScan.ScanGUI
{
    public class ScanOverviewDataRenderer
    {
        private Vector2 scrollPosition;

        private ScanOverviewGUI[] scanOverviewGUIs;

        public ScanOverviewDataRenderer()
        {
            LoadData();
        }

        private void LoadData()
        {
            ScanOverviewData[] source = Singleton<ScanOverviewManager>.Instance.ReadScanResults();
            source = source.OrderByDescending((ScanOverviewData d) => DateTime.Parse(d.Time)).ToArray();
            scanOverviewGUIs = new ScanOverviewGUI[source.Length];
            for (int i = 0; i < source.Length; i++)
            {
                scanOverviewGUIs[i] = new ScanOverviewGUI(source[i]);
            }
        }

        public void DrawGUI(Rect position, bool scrollView = true, bool openSettingBtn = false)
        {
            if (scanOverviewGUIs == null || scanOverviewGUIs.Length == 0)
            {
                GUILayout.BeginVertical(GUILayout.ExpandHeight(expand: true));
                GUIStyle style = new GUIStyle(GUI.skin.label)
                {
                    fontSize = 26,
                    alignment = TextAnchor.MiddleCenter
                };
                GUILayout.Space(100f);
                GUILayout.FlexibleSpace();
                GUILayout.Label("暂无任何扫描结果", style);
                GUILayout.FlexibleSpace();
                GUILayout.EndVertical();
                return;
            }

            if (scrollView)
            {
                scrollPosition = GUILayout.BeginScrollView(scrollPosition, new GUIStyle(), GUI.skin.verticalScrollbar, GUILayout.Width(position.width), GUILayout.Height(position.height));
            }

            GUILayout.BeginHorizontal();
            GUILayout.Space(20f);
            if (GUILayout.Button("折叠全部", GUILayout.Width(90f), GUILayout.Height(28f)))
            {
                ScanOverviewGUI[] array = scanOverviewGUIs;
                foreach (ScanOverviewGUI scanOverviewGUI in array)
                {
                    scanOverviewGUI.IsShowScanResultsSetting = false;
                }
            }

            GUILayout.Space(20f);
            if (GUILayout.Button("展开全部", GUILayout.Width(90f), GUILayout.Height(28f)))
            {
                ScanOverviewGUI[] array2 = scanOverviewGUIs;
                foreach (ScanOverviewGUI scanOverviewGUI2 in array2)
                {
                    scanOverviewGUI2.IsShowScanResultsSetting = true;
                }
            }

            if (openSettingBtn)
            {
                GUILayout.Space(20f);
                if (GUILayout.Button("主窗口", GUILayout.Width(90f), GUILayout.Height(28f)))
                {
                    ScanSettingWindow.OpenScanSettingWindow();
                }
            }

            GUILayout.EndHorizontal();
            GUILayout.BeginVertical();
            for (int k = 0; k < scanOverviewGUIs.Length; k++)
            {
                scanOverviewGUIs[k].DrawGUI(position.width);
                GUILayout.Space(4f);
            }

            GUILayout.EndVertical();
            if (scrollView)
            {
                GUILayout.EndScrollView();
            }
        }
    }
}